﻿using ATEC.GeometryNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using SSDPNET.SZEWObject;
using SSDPNET.Geom;
using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATObjects;
using SZEWAlignmentNet.AlignmentBaseClass;
using SZEWAlignmentNet.AlignmentInterface;
using SZEWAlignmentNet.SpatialAlignmentInfo;

namespace DoModelExample
{
    public class AlignmentUtilMethod
    {
        private static SZEWAlignmentNet.AlignmentBaseClass.AlignmentInfo GetInfoByID(ObjectId id)
        {
            List<SZEWAlignmentNet.AlignmentBaseClass.AlignmentInfo> allInfo = new List<SZEWAlignmentNet.AlignmentBaseClass.AlignmentInfo>();
            if (!SZEWAlignmentInterface.GetAllAlignmentInfo(allInfo))
                return null;
            if (allInfo.Count == 0)
                return null;
            for(int i=0;i<allInfo.Count;i++)
            {
                if(allInfo[i].Id==id)
                {
                    return allInfo[i];
                }
            }
            return null;
        }

        public static XTransform3d CalcTransformForProfile(double stationToCalc, double dOffset_M =0.0)
        {
            XPoint3d ptInAlignment = new XPoint3d();
            //前进方向的向量
            XVector3d tangentVec = new XVector3d();
            ComponentInfo component = CreateComponentExample.componentModel;
            long idLong = component.component.RoadInfo.RoadId;
            ObjectId alignId = new ObjectId(ref idLong);

            //获取接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(GetInfoByID(alignId));
            LocationDetailInfo locationDetailInfo = new LocationDetailInfo();
            if (alignmentInterface.GetLocationDetailByStation(locationDetailInfo, stationToCalc.ToString()))
            {
                ptInAlignment = locationDetailInfo.point;
                tangentVec = locationDetailInfo.tangent;
            }
            tangentVec.NormalizeInPlace();

            // 叉积，得到法向;
            XVector3d noraml = tangentVec.CrossProduct(XVector3d.UnitZ); 
            noraml.NormalizeInPlace();
            XMatrix3d rot = new XMatrix3d();
            XMatrix3d.TryOrthogonalFrameFromXYVectors(tangentVec, noraml, out rot);

            XVector3d X;
            XVector3d Y;
            XVector3d Z;
            rot.GetColumns(out X, out Y, out Z);

            XMatrix3d.TryOrthogonalFrameFromXYVectors(Y, Z, out rot);

            XPoint3d basePoint = ptInAlignment;
            XVector3d move = basePoint - XPoint3d.Zero;
            XTransform3d trsfm = XTransform3d.FromMatrixAndTranslation(rot, move);
            return trsfm;
        }

    }


}
